/** This class populates discount item records with unit product price
*     from price Book entry of North American PriceBook
*   
*     Author - Sayeed Faruqi
*     Company - Cognizant Technology Solutions
*     Client - Cisco (LinkSys)
*     
*     Last update - Oct 2009
* 
**/

global class NAUnitPrice { 
    
    // This method retrieves and updates the unit price of selected discount item product
    webservice static void getPrice( discountItems__c[] disc){
       
       // Let's first get the Name of the North America retail price book from
       // Linskys Sys parameter object. This step is required to avoid hard coding the
       // price book name in this class
       string NAPriceBookName = [select paramvalue__c
                                 from SPAConfig__c 
                                 where param_name__c = 'NA Retail Price Book'].paramvalue__c; 
       
       // Get North American Price Book Id from Price Book
       string NAPriceBookId = [select id 
                               from pricebook2
                               where name = :NAPriceBookName             
                               LIMIT 1].id ;
      
      // In this FOR loop below we are going to set the unitprice field on discount item
      // records as North American Unit Price of the select discount item product
      // the unit prices are updated only if SPA request has NOT been approved
       
        try {    
            for (discountitems__c d:disc) {
                    if (d.SPAstatus__c <> 'Approved') {
                        d.NAunitprice__c = [select unitprice
                                                        from PriceBookEntry pb
                                                        where pb.product2id = :d.PartNumber__c
                                                         and pb.priceBook2Id = :NAPriceBookId
                                                         LIMIT 1 ].unitprice;
                                                     
                        // Let's set the discounted unit price based on on the percentages 
                        // or dollar amount specified in discount line item record
                        if (d.Is_Discount_inDollars__c == TRUE ) {
                            // When discount is specified in dollars per unit
                            d.discounted_unit_price__c = d.NAUnitPrice__c - d.discount__c;
                            system.debug('calculating in $');
                        } else {
                            // When discounted is specified as % of unit price
                            d.discounted_unit_price__c = d.NAUnitPrice__c * (1-(d.discount__c/100));
                            system.debug('calculating in %');
                        }
                    } //End IF
            } // End For
        } //End Try
        catch (system.QueryException e) {
            
                //list process exceptions
                system.debug ('Error encountered in NAUnitPrice Apex Class ' + e.getMessage());
            
        } //end catch
    } // End getPrice Method
    
    //Below is the test method for this class
    @isTest
    static void NAUnitPriceTest(){
        discountItems__c[] discTest = new discountItems__c[]{
            new discountItems__c(partnumber__c='01tQ0000000i4nL',Is_Discount_inDollars__c= TRUE,discount__c=10),
            new discountItems__c(partnumber__c='01tQ0000000i4nL',Is_Discount_inDollars__c= FALSE,discount__c=20)
         };
        NAUnitPrice.getprice(discTest);
    }
} // End of NAUnitPrice Apex Class